package info.xiao.dataStructrue.array;

/**
 * @Classname CheckPossibility
 * @Description 非递减数列
 * @Href https://leetcode.cn/problems/non-decreasing-array/
 * @Date 2023/3/10 10:08
 * @Created by 晓龙Oba
 */
public class CheckPossibility {
    public static void main(String[] args) {
        int[] nums = {1, 5, 4, 6, 7, 10, 8, 9};
        Boolean result = checkPossibility(nums);
        System.out.println(result);
    }

    private static Boolean checkPossibility(int[] nums) {
        // 设置计数器
        int times = 0;
        for (int i = 0; i < nums.length - 1; i++) {
            // 判断元素下标
            if (nums[i] > nums[i + 1]) {
                if (i == 0 || i + 1 == nums.length - 1) {
                    times++;
                    continue;
                }
                // 判断非递减数列特征
                if (nums[i - 1] > nums[i + 1]) {
                    if (nums[i] > nums[i + 2]) {
                        return false;
                    }
                }
                times++;
            }
        }

        return times < 2;
    }
}
